﻿using System;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SymulatorEthernetWPF.Shared;

namespace SymulatorEthernetWPF.SimObjects.Wires
{
    /// <summary>
    /// Interaction logic for WireBenderHandler.xaml
    /// </summary>
    /// 
    [Serializable]
    public partial class WireBenderHandler : UserControl, ISerializable
    {
        
        private bool _isMoving = true;
        public Wire Wire;
        private double _x;
        private double _y;

        public WireBenderHandler(Wire wire, double x, double y)
        {
            Simulation.UndoStack.Push(new HistoryElement());
            Simulation.RedoStack.Clear();
            InitializeComponent();
            Wire = wire;
            Position = new Point(x, y);
            StaticVars.MainWindow.scrollViewer.MouseMove += new MouseEventHandler(updateWireBenderHandler);
            
        }

        public WireBenderHandler(SerializationInfo info, StreamingContext context)
        {
            InitializeComponent();
            Wire = (Wire) info.GetValue("wire", typeof (Wire));
            Position = (Point) info.GetValue("position", typeof (Point));
            _isMoving = false;
        }

        public Point Position
        {
            get { return new Point(Canvas.GetLeft(this) + (ActualWidth/2), Canvas.GetTop(this) + (ActualHeight/2)); }
            set
            {
                _x = value.X;
                _y = value.Y;
                Canvas.SetLeft(this, _x - (ActualWidth/2));
                Canvas.SetTop(this, _y - (ActualHeight/2));
            }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("position", Position);
            info.AddValue("wire", Wire);
        }

        #endregion

        private void UserControlLoaded(object sender, RoutedEventArgs e)
        {
            Canvas.SetLeft(this, _x - (ActualWidth/2));
            Canvas.SetTop(this, _y - (ActualHeight/2));
        }


        private void ContextMenuItemUsunClick(object sender, RoutedEventArgs e)
        {
            Wire.WireBenderHandlerList.Remove(this);
            StaticVars.MainWindow.customCanvas.Children.Remove(this);
            //LogicRunner.UIElementsList.Remove(this);
            Wire.X1++;
            Wire.X1--;
            Wire.updateWire();
        }

        private void EllipseMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Simulation.UndoStack.Push(new HistoryElement());
            Simulation.RedoStack.Clear();
            _isMoving = true;
            StaticVars.MainWindow.scrollViewer.MouseMove+=new MouseEventHandler(updateWireBenderHandler);
        }

        public void updateWireBenderHandler(object sender = null, MouseEventArgs e = null)
        {
            if (_isMoving)
            {
                Position = Mouse.GetPosition(StaticVars.MainWindow.customCanvas);
                Wire.X1++;
                Wire.X1--;
            }
        }

        private void EllipseMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isMoving = false;
            StaticVars.MainWindow.scrollViewer.MouseMove -= new MouseEventHandler(updateWireBenderHandler);
        }

        public void DoLogic()
        {
            
        }
    }
}